import { operateRepairList, repairRowConfig } from '@/views/print/mesOtherCodePrinting/config';
import {
  addRepairPrint,
  delRepairReport,
  getRepairPrintList,
  reprintSel,
  reprintSelList
} from '@/api/print/mesProdOthercode';
import { getDict } from '@/api/common';
import { setPrintData } from '@/utils/judge';

export default {
  data() {
    return {
      repairListDialogVisible: false,  // 补打列表抽屉显示/隐藏
      repairPrintDialogVisible: false,  // 补打表单弹窗显示/隐藏
      printNumberVisible: false,  // 打印数量弹窗显示/隐藏
      repairFormDataDialog: {},  // 补单表单数据
      printFormDataDialog: {
        customerCode: null,
      },  // 打印表单数据
      operateRepairList,  // 补打申请列表上方的操作按钮
      getRepairPrintList,  // 补打申请列表接口
      initRepairForm: {},  // 初始化补打列表查询参数
      repairColumn: [
        {
          type: "selection",
        },
        {
          type: "index",
          label: $i18n.t("common.order"),
          width: 60,
          fixed: "left",
        },
        // 型号编码
        {
          prop: "orderCode",
          label: $i18n.t("mesOtherCodePrinting.orderCode"),
          width: 180,
        },
        {
          prop: "codeType",
          label: $i18n.t("mesOtherCodePrinting.codeType"),
          width: '100',
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            const list = await getDict('mes_code_type');
            return list.data.result;
          },
        },
        {
          prop: "startBarcode",
          label: $i18n.t("mesOtherCodePrinting.startBarcode"),
          width: 120,
        },
        {
          prop: "endBarcode",
          label: $i18n.t("mesOtherCodePrinting.endBarcode"),
          width: 120,
        },
        {
          prop: "printTemp",
          label: $i18n.t("mesOtherCodePrinting.printTemp"),
          width: 100,
        },
        {
          prop: "createByName",
          label: $i18n.t('mesOtherCodePrinting.createByName'),
          width: 100,
        },
        {
          prop: "createTime",
          label: $i18n.t('mesOtherCodePrinting.createTime'),
          width: 140,
        },
        {
          prop: "status",
          label: $i18n.t("mesOtherCodePrinting.status"),
          width: 100,
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            const list = await getDict('mes_barcode_reprint_status');
            return list.data.result;
          },
        },
        {
          prop: "examineByName",
          label: $i18n.t("mesOtherCodePrinting.examineByName"),
          width: 100,
        },
        {
          prop: "examineTime",
          label: $i18n.t("mesOtherCodePrinting.examineTime"),
          width: 140,
        },
        {
          prop: "examineContent",
          label: $i18n.t("mesOtherCodePrinting.examineContent"),
          width: 100,
        },
        {
          prop: "isPrint",
          label: $i18n.t("mesOtherCodePrinting.isPrint"),
          width: 100,
          optionProp: {
            value: "value",
            label: "description",
          },
          option: async (data) => {
            const list = await getDict('has_printed');
            return list.data.result;
          },
        },
        {
          prop: "operate",
          label: $i18n.t("common.operation"),
          width: 120,
          fixed: "right",
          render: (h, scope) => {
            return (
              <ele-row-operation
                scope={scope}
                rowConfig={repairRowConfig}
                onRowOperate={this.repairRowOperate}
              ></ele-row-operation>
            );
          },
        },
      ],
      printParams: {},  // 打印参数
      isNeedCheck: false,  // 补打是否需要审核，true:需要审核  false:不需要审核
      maxPrintNumber: 1,  // 最大打印数量
    }
  },
  methods: {
    // 补打表格上方操作按钮事件
    handleOperateRepairEvent({ handleName, name, rows }) {
      this.title = name;
      let _this = this;
      const fnc = {
        add: () => {
          this.repairFormDataDialog = { ...this.initRepairForm };
          this.repairPrintDialogVisible = true;
        },
      };
      fnc[handleName] && fnc[handleName]();
    },
    // 补打申请提交
    handleSubmitRepair(data) {
      if (this.isNeedCheck) {
        addRepairPrint(data).then(res => {
          this.refreshRepairTable();
          this.$message.success($i18n.t("common.addSuccess"));
        }).finally(() => this.repairPrintDialogVisible = false)
      } else {
        this.repairRowOperate({
          handleName: 'print',
          row: data,
        });
      }
    },
    // 补打申请表格行操作事件
    repairRowOperate({ handleName, name, row }) {
      this.title = name;
      const fnc = {
        delete: () => {
          delRepairReport(row.id).then(res => {
            this.$message.success($i18n.t("common.deleteSuccess"));
            this.refreshRepairTable();
          })
        },
        print: () => {
          const params = {
            masterId: row['id'],
            orderCode: row['orderCode'],
            codeType: row['codeType'],
            startBarCode: row['startBarcode'],
            endBarCode: row['endBarcode']
          };
          reprintSelList(params).then(res => {
            if (res.data.result.length) {
              if (this.isNeedCheck) {
                setPrintData(
                  {
                    code: row['printTempCode'],
                    name: row['printTemp'],
                    status: 1,  // 0:打印，1:补打
                  },
                  res.data.result,
                );
              } else {
                setPrintData(
                  {
                    code: res.data.result[0]['barcodePrintTempCode'],
                    name: res.data.result[0]['barcodePrintTemp'],
                    status: 1,  // 0:打印，1:补打
                  },
                  res.data.result,
                );
              }
              this.repairPrintDialogVisible = false;
              this.repairListDialogVisible = false;
              this.$router.push("/splitPrint");
            } else {
              this.$message.error($i18n.t("mesOtherCodePrinting.noOtherCode"));
            }
          })
        },
      };
      fnc[handleName] && fnc[handleName]();
    },
    // 刷新补打列表表格数据
    refreshRepairTable(isReset = true) {
      this.$refs.repairPrintTable.refresh(isReset);
    },
  },
  computed: {
    // 补打表单配置
    repairFormDescDialog() {
      const _this = this;
      return {
        span: 24,
        isShowBackBtn: false,
        isShowSubmitBtn: true,
        submitBtnText: this.repairListDialogVisible ? $i18n.t('common.ok') : $i18n.t('common.print'),
        labelPosition: "right",
        formDesc: {
          orderCode: {
            type: "input",
            label: $i18n.t("mesOtherCodePrinting.orderCode"),
            layout: 12,
            disabled: true,
          },
          codeType: {  // 分类
            type: "select",
            label: $i18n.t("mesOtherCodePrinting.codeType"),
            layout: 12,
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getDict("mes_code_type");
              return list.data.result;
            },
            on: {
              change: async (val, data) => {
                if (val) {
                  const params = {
                    orderCode: _this.repairListDialogVisible ? _this.initRepairForm.orderCode : _this.repairFormDataDialog['orderCode'],
                    codeType: val,
                  };
                  const response = await reprintSel(params);
                  if (response.data.result['quantity'] === 0) {
                    this.$message.error($i18n.t("mesOtherCodePrinting.theCurrentWorkOrderCannotBeReplenishedByOtherCodes"));
                    return;
                  }
                  this.$set(this.repairFormDataDialog, 'quantity', response.data.result['quantity']);
                  this.$set(this.repairFormDataDialog, 'startBarcode', response.data.result['startBarCode']);
                  this.$set(this.repairFormDataDialog, 'endBarcode', response.data.result['endBarCode']);
                }
              }
            },
            required: true,
            disabled: false,
          },
          prodCode: {  // 型号编码
            type: "input",
            label: $i18n.t("mesOtherCodePrinting.prodCode"),
            layout: 12,
            disabled: true,
          },
          prodName: {  // 型号名称
            type: "input",
            label: $i18n.t("mesOtherCodePrinting.prodName"),
            layout: 12,
            disabled: true
          },
          quantity: {  // 条码数量
            type: "number",
            label: $i18n.t("mesOtherCodePrinting.printQuantity"),
            layout: 12,
            disabled: true
          },
          startBarcode: {  // 补打起始条码
            type: "input",
            label: $i18n.t('mesOtherCodePrinting.startBarCode'),
            layout: 24,
            required: true,
          },
          endBarcode: {  // 补打终止条码
            type: "input",
            label: $i18n.t('mesOtherCodePrinting.endBarCode'),
            layout: 24,
            required: true,
          },
        },
      }
    },
    // 打印数量表单配置
    printNumberFormDescDialog() {
      return {
        span: 24,
        isShowBackBtn: false,
        isShowSubmitBtn: true,
        submitBtnText: $i18n.t('common.print'),
        labelPosition: 'left',
        formDesc: {
          amount: {
            type: "number",
            label: $i18n.t("mesOtherCodePrinting.amount"),
            layout: 16,
            attrs: {
              min: 1,
              max: this.maxPrintNumber,
            }
          },
        }
      };
    }
  }
}

